//
//  KnowDatilViewController.m
//  HealthA段
//
//  Created by MrJXT on 15/6/6.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "KnowDatilViewController.h"
#import "KnowDatil.h"
#import "KnowDatilCell.h"
#import "MessageCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "UMSocial.h"
#import "dataBaseHandle.h"
#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
@interface KnowDatilViewController ()<UIWebViewDelegate,UMSocialUIDelegate>
@property(nonatomic,retain)UIWebView *myView;
@property(nonatomic,retain)NSMutableDictionary *knowDic;
@property(nonatomic,retain)KnowDatil *know;
@property(nonatomic,assign)BOOL isclick;
@property(nonatomic,retain)UIButton *bu1;
@property(nonatomic,retain)UIButton *bu2;
@property(nonatomic,retain)UIImageView *myimage;
@property(nonatomic,assign)NSInteger num;
@property(nonatomic,retain)NSMutableArray *arr;
//创建分享收藏界面view
@property(nonatomic,retain)UIView *roundView;
//@property(nonatomic,retain)UIButton *roundBu;
@property(nonatomic,retain)UIButton *top;
@end

@implementation KnowDatilViewController
- (void)dealloc
{
    [_knowDic release];
    [_myView release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.knowDic = [NSMutableDictionary dictionary];
        self.arr = [NSMutableArray array];
        self.isclick = NO;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatWeb];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.myView.scrollView.contentInset = UIEdgeInsetsMake(340, 0, 0, 10);
    self.myimage  = [[UIImageView alloc]initWithFrame:CGRectMake(0, -340, WIDTH, 280)];
    self.myView.scrollView.backgroundColor = [UIColor whiteColor];
    self.myView.scrollView.pagingEnabled = YES;
    [self PleaseData];
    
    //创建分享，收藏界面
    self.roundView = [[UIView alloc]initWithFrame:CGRectMake(0, -60, WIDTH, 60)];
    [self.myView.scrollView addSubview:self.roundView];
    self.roundView.layer.borderWidth = 1;
    self.roundView.layer.cornerRadius = 8;
    self.roundView.layer.masksToBounds = YES;
    self.roundView.layer.borderColor = [UIColor clearColor].CGColor;
    self.roundView.backgroundColor = [UIColor whiteColor];
    self.roundView.alpha = 0.8;
    [_roundView release];
    
    //创建返回顶部按钮
    self.top = [UIButton buttonWithType:UIButtonTypeCustom];
    self.top.frame = CGRectMake(0.8 * WIDTH, 0.7 *HEIGHT, 40, 40);
    [self.top setTitle:@"" forState:UIControlStateNormal];
    [self.top addTarget:self action:@selector(clickToTop:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:self.top];
    [self.top setBackgroundImage:[UIImage imageNamed:@"iconfont-iconfontxiangxiajiantoujian"] forState:UIControlStateNormal];
    self.top.alpha = 0.43;
}

//数据请求
-(void)PleaseData
{
    NSString *url = [NSString stringWithFormat:@"http://api.yi18.net/lore/show?id=%ld",(long)self.id];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        self.knowDic = [dic objectForKey:@"yi18"];
        KnowDatil *kd = [[KnowDatil alloc]init];
        [kd setValuesForKeysWithDictionary:self.knowDic];
        self.know = kd;
        [self creatButtonView];
        NSString *str = [NSString stringWithFormat:@"http://www.yi18.net/%@",[self.knowDic objectForKey:@"img"]];
        NSURL *url = [NSURL URLWithString:str];
        [self.myimage sd_setImageWithURL:url];
        [self.myView loadHTMLString:self.know.message baseURL:nil];
        [self.myView.scrollView addSubview:self.myimage];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
        
    }];
}
-(void)creatWeb
{
    
    self.myView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 + 40)];
    self.myView.delegate = self;
    self.myView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myView];
    [_myView release];
    
}

//创建button
-(void)creatButtonView
{
    self.bu1 = [UIButton buttonWithType:UIButtonTypeSystem];
    self.bu1.frame = CGRectMake(0.28 *WIDTH, 10, 40, 40);
//    self.bu1.tintColor = [UIColor purpleColor];
//    self.bu1.layer.borderWidth = 1;
//    self.bu1.layer.cornerRadius = 12;
//    self.bu1.layer.masksToBounds = YES;
//    self.bu1.layer.borderColor = [UIColor clearColor].CGColor;
//    self.bu1.backgroundColor = [UIColor grayColor];
    [self.bu1 setBackgroundImage:[UIImage imageNamed:@"iconfont-701"] forState:UIControlStateNormal];
    [self.bu1 addTarget:self action:@selector(clickTobu1:) forControlEvents:UIControlEventTouchUpInside];
    [self.roundView addSubview:self.bu1];
    [self.view bringSubviewToFront:self.bu1];
    [self.view sendSubviewToBack:self.myView];
    
    
    self.bu2 = [UIButton buttonWithType:UIButtonTypeSystem];
    self.bu2.frame = CGRectMake(0.63 *WIDTH, 10, 40, 40);
//    self.bu2.layer.borderWidth = 1;
//    self.bu2.layer.cornerRadius = 12;
//    self.bu2.layer.masksToBounds = YES;
//    self.bu2.layer.borderColor = [UIColor clearColor].CGColor;
////    self.bu2.backgroundColor = [UIColor grayColor];
//    self.bu2.tintColor = [UIColor purpleColor];
    [self.bu2 setBackgroundImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:UIControlStateNormal];
    [self.bu2 addTarget:self action:@selector(clickBu2:) forControlEvents:UIControlEventTouchUpInside];
    [self.roundView addSubview:self.bu2];
    [self.view bringSubviewToFront:self.bu2];
    [self.view sendSubviewToBack:self.myView];
    
    
}

//收藏按钮
-(void)clickTobu1:(UIButton *)bu
{
    //打开数据库
    [[dataBaseHandle shareDB]openDB];
    //创建表
    [[dataBaseHandle shareDB]creatKnowTable];
    self.arr = [[dataBaseHandle shareDB] selectKnowTable];
    if (self.arr.count > 0) {
        for (KnowDatil *playmo in self.arr) {
            if ([playmo.title isEqual:self.know.title]) {
                [self.bu1 setTitle:@"已收藏" forState:UIControlStateNormal];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                self.num = 1;
                [alert show];
                [alert release];
                break;
            }
        }
        if (self.num != 1) {
            [[dataBaseHandle shareDB]insertKnowthWithTitle:self.know.title imgUrl:[NSString stringWithFormat:@"http://www.yi18.net/%@",self.know.img] id:self.know.id];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
    if (self.arr.count == 0) {
        [[dataBaseHandle shareDB]insertKnowthWithTitle:self.know.title imgUrl:[NSString stringWithFormat:@"http://www.yi18.net/%@",self.know.img] id:self.know.id];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
//分享按钮
-(void)clickBu2:(UIButton *)bu
{
    if (self.isclick == NO) {
        self.bu1.alpha = 0.56;
        self.bu2.alpha = 01.0;
        self.isclick = YES;
    }else if (self.isclick == YES){
        self.bu2.alpha = 1.0;
        self.bu1.alpha = 0.56;
        self.isclick = NO;
    }
    self.bu1.alpha = 0.7;
//    self.bu1.backgroundColor = [UIColor grayColor];

    NSString *image = [NSString stringWithFormat:@"http://www.yi18.net/%@",self.know.img];
    NSString *text = self.know.title;
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"55406a3ce0f55a7bd5002d90" shareText:text shareImage:image shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren, nil] delegate:self];
    NSLog(@"分享");
}

-(void)clickToTop:(UIButton *)bu
{
    if (self.isclick == NO) {
        self.top.alpha = 0.88;
        self.isclick = YES;
    }
    [self.myView.scrollView setContentOffset:CGPointMake(0, -340) animated:YES];
}

//tabar隐藏
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
